﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using PRET.ReportControlLib.ServiceReference;
using PRET.ReportDataDefine.UIData;

namespace PRET.ReportControlLib.ReportView
{
    public partial class EmployeAttendanceReportView : UserControl
    {
        private PRETWebServiceClient adapter = null;
        private Dictionary<string, EmployeAttendance[]> employDicOrg = null;
        private Dictionary<string, EmployeAttendance[]> employDicMod = null;

        public EmployeAttendanceReportView()
        {
            InitializeComponent();
            adapter = new PRETWebServiceClient();
            this.employeAttendanceFilter.showReportEvent += employeAttendanceFilter_showReportEvent;
            this.employeAttendanceFilter.showReportByTeamEvent += employeAttendanceFilter_showReportByTeamEvent;
        }

        void employeAttendanceFilter_showReportByTeamEvent(object sender, Controls.ShowAttendanceReportViewTeamNameEventArgs args)
        {
            try
            {
                this.employeAttendanceFilter.EnableControls(false);

                this.employeAttReportViewer1.LocalReport.DataSources.Clear(); //clear report
                if (employDicMod != null && employDicMod.Count > 0)
                {
                    if (employDicMod[args.Team] != null && employDicMod[args.Team].Length > 0)
                    {
                        Microsoft.Reporting.WinForms.ReportDataSource empDataset = new Microsoft.Reporting.WinForms.ReportDataSource("EmployeAttendanceDS"); // set the datasource
                        this.employeAttReportViewer1.LocalReport.DataSources.Add(empDataset);
                        empDataset.Value = employDicMod[args.Team];
                    }
                }
                this.employeAttReportViewer1.LocalReport.Refresh();
                this.employeAttReportViewer1.RefreshReport();

                this.employeAttReportViewer2.LocalReport.DataSources.Clear(); //clear report
                if (employDicOrg != null && employDicOrg.Count > 0)
                {
                    if (employDicOrg[args.Team] != null && employDicOrg[args.Team].Length > 0)
                    {
                        Microsoft.Reporting.WinForms.ReportDataSource empDataset = new Microsoft.Reporting.WinForms.ReportDataSource("EmployeAttendanceDS"); // set the datasource
                        this.employeAttReportViewer2.LocalReport.DataSources.Add(empDataset);
                        empDataset.Value = employDicOrg[args.Team];
                    }
                }
                this.employeAttReportViewer2.LocalReport.Refresh();
                this.employeAttReportViewer2.RefreshReport();
            }
            catch (Exception ex)
            {
                string msg = string.Format("EmployeAttendanceReportView.employeAttendanceFilter_showReportByTeamEvent() Failed: {0}.\r\nDetail: {1}", ex.Message, ex.StackTrace);
                MessageBox.Show(msg, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            { this.employeAttendanceFilter.EnableControls(true); }
        }

        void employeAttendanceFilter_showReportEvent(object sender, Controls.ShowAttendanceReportViewEventArgs args)
        {
            try
            {
                this.employeAttendanceFilter.EnableControls(false);
                if (this.adapter == null)
                {
                    MessageBox.Show("EmployeAttendanceReportView 没有初始化Server Adapter!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                Dictionary<string, EmployeAttendance[]>[] retList = this.adapter.GetAllDepEmployeAttendance(args.Month);
                if (retList == null || retList.Length == 0)
                    return;

                this.employDicMod = retList[1];
                this.employeAttReportViewer1.LocalReport.DataSources.Clear(); //clear report
                if (employDicMod != null && employDicMod.Count > 0)
                {
                    if (employDicMod[args.Team] != null && employDicMod[args.Team].Length > 0)
                    {
                        Microsoft.Reporting.WinForms.ReportDataSource empDataset = new Microsoft.Reporting.WinForms.ReportDataSource("EmployeAttendanceDS"); // set the datasource
                        this.employeAttReportViewer1.LocalReport.DataSources.Add(empDataset);
                        empDataset.Value = employDicMod[args.Team];
                    }
                }
                this.employeAttReportViewer1.LocalReport.Refresh();
                this.employeAttReportViewer1.RefreshReport();

                this.employDicOrg = retList[0];
                this.employeAttReportViewer2.LocalReport.DataSources.Clear(); //clear report
                if (employDicOrg != null && employDicOrg.Count > 0)
                {
                    if (employDicOrg[args.Team] != null && employDicOrg[args.Team].Length > 0)
                    {
                        Microsoft.Reporting.WinForms.ReportDataSource empDataset = new Microsoft.Reporting.WinForms.ReportDataSource("EmployeAttendanceDS"); // set the datasource
                        this.employeAttReportViewer2.LocalReport.DataSources.Add(empDataset);
                        empDataset.Value = employDicOrg[args.Team];
                    }
                }
                this.employeAttReportViewer2.LocalReport.Refresh();
                this.employeAttReportViewer2.RefreshReport();
            }
            catch (Exception ex)
            {
                string msg = string.Format("EmployeAttendanceReportView.employeAttendanceFilter_showReportEvent() Failed: {0}.\r\nDetail: {1}", ex.Message, ex.StackTrace);
                MessageBox.Show(msg, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            { this.employeAttendanceFilter.EnableControls(true); }
        }
    }
}
